package db

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
)

var Db *gorm.DB

func init() {
	//test:qsou@123456@tcp(dawang.com:3406)/go_admin?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
	mysqlstr := "test:qsou@123456@tcp(dawang.com:3406)/go_example?parseTime=true"
	log.Print("初始化启动-链接mysql: " + mysqlstr)
	var err error
	db, err := gorm.Open("mysql", mysqlstr)
	if err != nil {
		log.Fatal(err.Error())
	}

	// 表名非负数形式
	db.SingularTable(true)

	// 开启log 日志
	db.LogMode(true)
	// 自定义前缀
	gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
		//if(strings.HasPrefix(defaultTableName,"tb_")){
		//	defaultTableName = defaultTableName[3:len(defaultTableName)]
		//	return defaultTableName
		//}
		return "" + defaultTableName
	}

	Db = db
}
